﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;


using System.Web;
using Dingding.Helper;



namespace DingApi.Controllers
{
    [RoutePrefix("api/Pabm01")]
    public class Pabm01Controller : ApiController
    {


        public System.Data.DataSet GetAllPabm01()
        {
           return PostPabm01ById("摇表");
        }

        public System.Data.DataSet GetPabm01ById(string search_name, string user_id)
        {

            EI.EIInfo inBlock = new EI.EIInfo();

            //if (string.IsNullOrWhiteSpace(search_name))
            //{
            //    inBlock.sys_info.msg = "请输入资材名称";
            //    return inBlock;
            //}


            inBlock.Tables[0].Columns.Add("REC_CREATE_TIME");
            inBlock.Tables[0].Columns.Add("REC_REVISE_TIME");           


            inBlock.Tables[0].Rows.Add();
            //inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid = user_id;//需要从前台获取

            inBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = DateTime.Now.AddMonths(-1).ToString("yyyyMMdd");;
            inBlock.Tables[0].Rows[0]["REC_REVISE_TIME"] = DateTime.Now.AddDays(1).ToString("yyyyMMdd");
            

            EI.EIInfo outBlock = EI.EITuxedo.CallService("paam01_inq", inBlock);
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("FIBR","FIDP");
            DataTable dtTemp=null;

          
            for (int i = outBlock.Tables[0].Rows.Count -1 ; i >=0; i--)
            {
                if (outBlock.Tables[0].Rows[i]["REMARK"].ToString() == "对外销售出库时自动生成")
                {
                    outBlock.Tables[0].Rows[i].Delete();
                    continue;
                }
                   
                    	

                string branch_code = outBlock.Tables[0].Rows[i]["BRANCH_CODE"].ToString();
                if (branch_code.Trim()!="")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();
             
                if (dtTemp !=null && dtTemp.Rows.Count==1)                
                outBlock.Tables[0].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string dptment_code = outBlock.Tables[0].Rows[i]["DPTMENT_CODE"].ToString();
                 if (dptment_code.Trim()!="")
                dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();

                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                 string apply_date = outBlock.Tables[0].Rows[i]["APPLY_DATE"].ToString();
                 outBlock.Tables[0].Rows[i]["APPLY_DATE"] = apply_date.Substring(0, 4) + "-" + apply_date.Substring(4, 2) + "-" + apply_date.Substring(6, 2);


            }
           
//         

            outBlock.Tables[0].TableName = "Table0"; 
            return outBlock;

        }



        [HttpGet]

        [Route("GetPabm01Info")]
        public System.Data.DataSet GetPabm01Info(string purchase_apply_bill_no)
        {

            EI.EIInfo inBlock = new EI.EIInfo();



            inBlock.Tables[0].Columns.Add("PURCHASE_APPLY_BILL_NO");          
            inBlock.Tables[0].Rows.Add();

            inBlock.Tables[0].Rows[0]["PURCHASE_APPLY_BILL_NO"] = purchase_apply_bill_no ;


            EI.EIInfo outBlock = EI.EITuxedo.CallService("paam0102_inq", inBlock);
            EI.EIInfo outBlock_TEMP = EF.Utility.GetCodeClassValue("PA30","FIBR", "FIDP","PAA1","PA29");

            EI.EIInfo outBlock_TEMP2 = EF.Utility.ExecQuery("select t.STOCK_NO CODE ,t.stock_name as CODE_DESC_1_CONTENT from tsimm01 t");
            //

            DataTable dtTemp = null;

            if ( outBlock.Tables[0].Rows.Count==1)
            {
                string branch_code = outBlock.Tables[0].Rows[0]["BRANCH_CODE"].ToString();
                if (branch_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                string dptment_code = outBlock.Tables[0].Rows[0]["DPTMENT_CODE"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


                 string bill_status = outBlock.Tables[0].Rows[0]["BILL_STATUS"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA30"].Select("CODE  = '" + bill_status + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["BILL_STATUS"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

                //申请分类
                 string apply_classify = outBlock.Tables[0].Rows[0]["APPLY_CLASSIFY"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PA29"].Select("CODE  = '" + apply_classify + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["APPLY_CLASSIFY"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                
                //类型
                string apn = outBlock.Tables[0].Rows[0]["APN"].ToString();
                if (dptment_code.Trim() != "")
                    dtTemp = outBlock_TEMP.Tables["PAA1"].Select("CODE  = '" + apn + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["APN"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                
                 
                string stock_no = outBlock.Tables[0].Rows[0]["STOCK_NO"].ToString();
                if (stock_no.Trim() != "")
                    dtTemp = outBlock_TEMP2.Tables[0].Select("CODE  = '" + stock_no + "'").CopyToDataTable<DataRow>();
                if (dtTemp != null && dtTemp.Rows.Count == 1)
                    outBlock.Tables[0].Rows[0]["STOCK_NO"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];
                

                string apply_date = outBlock.Tables[0].Rows[0]["APPLY_DATE"].ToString();
                if (apply_date.Length >= 8)
                outBlock.Tables[0].Rows[0]["APPLY_DATE"] = apply_date.Substring(0, 4) + "-" + apply_date.Substring(4, 2) + "-" + apply_date.Substring(6, 2);

                string delivy_expected_date = outBlock.Tables[0].Rows[0]["DELIVY_EXPECTED_DATE"].ToString();
                if (delivy_expected_date.Length>=8)
                outBlock.Tables[0].Rows[0]["DELIVY_EXPECTED_DATE"] = delivy_expected_date.Substring(0, 4) + "-" + delivy_expected_date.Substring(4, 2) + "-" + delivy_expected_date.Substring(6, 2);

                string rec_create_time = outBlock.Tables[0].Rows[0]["REC_CREATE_TIME"].ToString();
                if (rec_create_time.Length >= 12)
                    outBlock.Tables[0].Rows[0]["REC_CREATE_TIME"] = rec_create_time.Substring(0, 4) + "-" + rec_create_time.Substring(4, 2) + "-" + rec_create_time.Substring(6, 2) + " " + rec_create_time.Substring(8, 2)  +":" + rec_create_time.Substring(10, 2);

                

            }
               



            //for (int i = 0; i < outBlock.Tables[0].Rows.Count; i++)
            //{
            //    string branch_code = outBlock.Tables[0].Rows[i]["BRANCH_CODE"].ToString();
            //    if (branch_code.Trim() != "")
            //        dtTemp = outBlock_TEMP.Tables["FIBR"].Select("CODE  = '" + branch_code + "'").CopyToDataTable<DataRow>();

            //    if (dtTemp != null && dtTemp.Rows.Count == 1)
            //        outBlock.Tables[0].Rows[i]["BRANCH_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];


            //    string dptment_code = outBlock.Tables[0].Rows[i]["DPTMENT_CODE"].ToString();
            //    if (dptment_code.Trim() != "")
            //        dtTemp = outBlock_TEMP.Tables["FIDP"].Select("CODE  = '" + dptment_code + "'").CopyToDataTable<DataRow>();

            //    if (dtTemp != null && dtTemp.Rows.Count == 1)
            //        outBlock.Tables[0].Rows[i]["DPTMENT_CODE"] = dtTemp.Rows[0]["CODE_DESC_1_CONTENT"];

            //    string apply_date = outBlock.Tables[0].Rows[i]["APPLY_DATE"].ToString();
            //    outBlock.Tables[0].Rows[i]["APPLY_DATE"] = apply_date.Substring(0, 4) + "-" + apply_date.Substring(4, 2) + "-" + apply_date.Substring(6, 2);


            //}

            ////         

            outBlock.Tables[0].TableName = "Table0";
            outBlock.Tables[1].TableName = "Table1";

            
            return outBlock;

        }

        public System.Data.DataSet GetPabm01Info_2(string purchase_apply_bill_no)
        {
            string str ="select t.remark1,t.rec_creator, ";
            str +=" substr(t.rec_create_time,1,4)||'-' || substr(t.rec_create_time,5,2)||'-'  ";
            str +=" ||substr(t.rec_create_time,7,2)  || ' ' ";
            str +=" ||substr(t.rec_create_time,9,2) || ':' ||substr(t.rec_create_time,11,2) ";
            str +=" rec_create_time   from tpabm29 t ";
            str += " where t.row_guid='" + purchase_apply_bill_no + "' order by rec_create_time desc ";

            EI.EIInfo outBlock = EF.Utility.ExecQuery(str);
            //


            outBlock.Tables[0].TableName = "Table0";
           


            return outBlock;

        }

      
         [HttpPost]
        public System.Data.DataSet PostPabm01ById(string asmat_name)
        {

            EI.EIInfo inBlock = new EI.EIInfo();

            if (string.IsNullOrWhiteSpace(asmat_name))
            {
                inBlock.sys_info.msg = "请输入资材名称";
                return inBlock;
            }


            inBlock.Tables[0].Columns.Add("STOCK_NO");
            inBlock.Tables[0].Columns.Add("FLAG");
            inBlock.Tables[0].Columns.Add("IS_FJ");
            inBlock.Tables[0].Columns.Add("ASMAT_NAME");


            inBlock.Tables[0].Rows.Add();

            inBlock.sys_info.company_code = "LT";
            inBlock.sys_info.userid = "admin";//需要从前台获取

            inBlock.Tables[0].Rows[0]["FLAG"] = " "; //库存金额不限制
            inBlock.Tables[0].Rows[0]["IS_FJ"] = "0"; //不包括废旧库
            inBlock.Tables[0].Rows[0]["ASMAT_NAME"] = asmat_name;

            EI.EIInfo outBlock = EI.EITuxedo.CallService("pasi15_inq", inBlock);
            return outBlock;

        }

         [HttpPost]
        public System.Data.DataSet PostAllPabm01()
        {
            EI.EIInfo inBlock = new EI.EIInfo();
            inBlock.Tables[0].Columns.Add("STOCK_NO");
            inBlock.Tables[0].Columns.Add("FLAG");
            inBlock.Tables[0].Columns.Add("IS_FJ");
            inBlock.Tables[0].Columns.Add("ASMAT_NAME");


            inBlock.Tables[0].Rows.Add();

            inBlock.sys_info.company_code           = "LT";
            inBlock.sys_info.userid                 = "admin";//需要从前台获取

            inBlock.Tables[0].Rows[0]["FLAG"]       = " "; //库存金额不限制
            inBlock.Tables[0].Rows[0]["IS_FJ"]      = "0"; //不包括废旧库
            inBlock.Tables[0].Rows[0]["ASMAT_NAME"] = "摇表";



            EI.EIInfo outBlock = EI.EITuxedo.CallService("pasi15_inq", inBlock);
            return outBlock;
           
        }
       
    }
}